<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .my-link{
            color: #ccc;
        }
        .router-link-active{
            color: #000;
        }
    
    </style>
</head>
<body>
    <div id="app">
        <router-link class="my-link" to="/user/32214">用户1</router-link>
        <router-link class="my-link" to="/user/456789">用户2</router-link>
        <router-link class="my-link" to="/user/90123">用户3</router-link>
        <router-view></router-view>
    </div>
</body>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script>
    // 1.声明路由
    let r1 = new VueRouter({
        routes:[
            {path:'/user/:id',component:{
                template:'<div>当前用户ID :{{$route.params.id}}</div>',
                // 第二种方法   推荐
                beforeRouteUpdate(to,from,next){
                    console.log('to',to);
                    console.log('to',from);
                    // 不希望下一步 不用 next();
                    next();
                }
            
            }}
        ]
    });
    // 2.调用路由

    let vm = new Vue({
        el:'#app',
        methods:{
                    
        },
        router:r1,
        // 第一种方法  (不推荐)
        watch:{
            '$route'(newVal,oldVal){
                alert(`${oldVal.params.id}->${newVal.params.id}`)
            }
        }
    })



</script>   
</html>